//
//  MKAPIRequestParam+AFNetworking.m
//  MKAPI
//
//  Created by 苏尚进 on 2022/9/8.
//  Copyright © 2022 moon. All rights reserved.
//

#import "MKAPIRequestParam+AFNetworking.h"

@implementation MKAPIRequestParam (AFNetworking)
- (NSMutableURLRequest *)af_requestWithError:(NSError **)error{
    AFHTTPRequestSerializer *requestSerializer = [AFHTTPRequestSerializer serializer];
    NSMutableURLRequest *request = [requestSerializer requestWithMethod:self.httpMethodString URLString:self.url parameters:nil error:error];
    if(self.HTTPBodyStream){
        request.HTTPBodyStream = self.HTTPBodyStream;
    }else if(self.HTTPBody){
        request.HTTPBody = self.HTTPBody;
    }
    [self.headersParam setHTTPHeaderFieldToRequest:request];
    return request;
}
@end


@implementation MKAPIRequestParamJSON (AFNetworking)
- (NSMutableURLRequest *)af_requestWithError:(NSError **)error{
    AFJSONRequestSerializer *requestSerializer = [AFJSONRequestSerializer serializer];
    NSMutableURLRequest *request = [requestSerializer requestWithMethod:self.httpMethodString URLString:self.url parameters:self.HTTPJsonBody error:error];
    [self.headersParam setHTTPHeaderFieldToRequest:request];
    return request;
}
@end
@implementation MKAPIRequestParamJSONArray (AFNetworking)
- (NSMutableURLRequest *)af_requestWithError:(NSError **)error{
    AFJSONRequestSerializer *requestSerializer = [AFJSONRequestSerializer serializer];
    NSMutableURLRequest *request = [requestSerializer requestWithMethod:self.httpMethodString URLString:self.url parameters:self.jsonArrayParam error:error];
    [self.headersParam setHTTPHeaderFieldToRequest:request];
    return request;
}
@end



@implementation MKAPIRequestParamForm (AFNetworking)
- (NSMutableURLRequest *)af_requestWithError:(NSError **)error{
    AFHTTPRequestSerializer *requestSerializer = [AFHTTPRequestSerializer serializer];
    NSMutableURLRequest *request = [requestSerializer requestWithMethod:self.httpMethodString URLString:self.url parameters:self.HTTPJsonFormBody error:error];
    [self.headersParam setHTTPHeaderFieldToRequest:request];
    return request;
}
@end

@implementation MKAPIRequestParamMultipartForm (AFNetworking)
- (NSMutableURLRequest *)af_requestWithError:(NSError **)error{
    AFHTTPRequestSerializer *requestSerializer = [AFHTTPRequestSerializer serializer];
    NSMutableURLRequest *request = [requestSerializer multipartFormRequestWithMethod:self.httpMethodString URLString:self.url parameters:self.HTTPJsonFormBody constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        for (MKAPIHttpFile *file in self.filesParam.files) {
            [formData appendPartWithFileData:file.data name:file.name fileName:file.fileName mimeType:file.mimeType];
        }
    } error:error];
    [self.headersParam setHTTPHeaderFieldToRequest:request];
    return request;
}
@end


@implementation MKAPIRequestParamOctetStream (AFNetworking)
- (NSMutableURLRequest *)af_requestWithError:(NSError **)error{
    AFHTTPRequestSerializer *requestSerializer = [AFHTTPRequestSerializer serializer];
    NSMutableURLRequest *request = [requestSerializer requestWithMethod:self.httpMethodString URLString:self.url parameters:nil error:error];
    if(self.fileParam.inputStream){
        request.HTTPBodyStream = self.fileParam.inputStream;
    }else if(self.fileParam.data){
        request.HTTPBody = self.fileParam.data;
    }
    [self.headersParam setHTTPHeaderFieldToRequest:request];
    return request;
}
@end

@implementation MKAPIRequestParamOriginRequest (AFNetworking)
- (NSMutableURLRequest *)af_requestWithError:(NSError **)error{
    NSMutableURLRequest *request = [self.originRequest mutableCopy];
    return request;
}
@end

